Skip to content

sql/opt: allow non-default NULLS ordering with SELECT DISTINCT#171980

Open
shafi-VM wants to merge 1 commit into
cockroachdb:masterfrom
shafi-VM:fix/168317-distinct-null-ordered-last
Open

sql/opt: allow non-default NULLS ordering with SELECT DISTINCT#171980
shafi-VM wants to merge 1 commit into
cockroachdb:masterfrom
shafi-VM:fix/168317-distinct-null-ordered-last

Conversation

@shafi-VM

Copy link
Copy Markdown

When null_ordered_last is enabled (or explicit NULLS FIRST/LAST is used), buildOrderBy synthesizes a (col IS NULL) ordering column ahead of columns with non-default NULL ordering. For plain SELECT DISTINCT, constructDistinct rejected these synthesized columns because they were not in the select list, causing a spurious:

for SELECT DISTINCT, ORDER BY expressions must appear in select list

error.

buildDistinctOn already handles this case by treating (col IS NULL) as functionally determined by col. This extracts that logic into a shared isSynthesizedNullsOrderingCol helper and reuses it in both constructDistinct and buildDistinctOn.

This fixes queries such as:

SET null_ordered_last = on;
SELECT DISTINCT a FROM t ORDER BY a;

It also fixes DROP USER and DROP ROLE, which internally execute a SELECT DISTINCT ... ORDER BY against system.privileges.

Fixes #168317
Epic: none

Release note (bug fix): Fixed a bug that caused SELECT DISTINCT ... ORDER BY queries to fail with a "for SELECT DISTINCT, ORDER BY expressions must appear in select list" error when null_ordered_last was enabled. The same issue also caused DROP USER and DROP ROLE to fail because they execute such queries internally.

When null_ordered_last is enabled (or an explicit NULLS FIRST/LAST is given),
buildOrderBy synthesizes a (col IS NULL) ordering column ahead of each column
with non-default NULLS ordering. For a plain SELECT DISTINCT, constructDistinct
rejected any ordering column not in the select list, so these synthesized
columns triggered a spurious

  for SELECT DISTINCT, ORDER BY expressions must appear in select list

error. The DISTINCT ON path (buildDistinctOn) already tolerates these columns
(case 3): a (col IS NULL) expression is functionally determined by col, so when
col is a grouping column the synthesized column can be treated as one too
without changing the result.

This extracts that check into a shared isSynthesizedNullsOrderingCol helper and
uses it from both constructDistinct and buildDistinctOn. It fixes user queries
such as

  SET null_ordered_last = on;
  SELECT DISTINCT a FROM t ORDER BY a;

as well as DROP USER and DROP ROLE, which internally run a
SELECT DISTINCT ... ORDER BY against system.privileges. Fixing constructDistinct
rather than suppressing the session setting for internal queries is the correct
layer, since the error reproduces with the plain user query above.

Fixes cockroachdb#168317
Epic: none

Release note (bug fix): Fixed a bug that caused SELECT DISTINCT ... ORDER BY
queries to fail with a "for SELECT DISTINCT, ORDER BY expressions must appear
in select list" error when the null_ordered_last session setting was enabled.
The same error caused DROP USER and DROP ROLE statements to fail, since they
run such a query internally.
@shafi-VM shafi-VM requested a review from a team as a code owner June 25, 2026 01:27
@shafi-VM shafi-VM requested review from andyyang890 and removed request for a team June 25, 2026 01:27
@blathers-crl

blathers-crl Bot commented Jun 25, 2026

Copy link
Copy Markdown

Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR.

My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@blathers-crl blathers-crl Bot added the O-community Originated from the community label Jun 25, 2026
@cockroachlabs-cla-agent

cockroachlabs-cla-agent Bot commented Jun 25, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@aerfrei aerfrei added the T-sql-queries SQL Queries Team label Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-community Originated from the community T-sql-queries SQL Queries Team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DROP USER fails when null_ordered_last is on

2 participants